/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$("document").ready(function(){
    $("#refeshNhomSanPham").bind("click", function(){
        layDanhSachNhomSanPham();
    });
    $("#nhomSanPham").bind("change", function(){
        layChiTietSanPham();
    });
    onKhongThemNhaSanXuat();
    onKhongThemNhaPhanPhoi();
    layDanhSachNhomSanPham();
    layDanhSachDanhMucSanPham();
    
});

function layChiTietSanPham(){
    var nhomSanPham = $("#nhomSanPham").val();        
    if (nhomSanPham == "ThemMoi"){
        var fxstring = javafxString({
            archive: "subPages/ThemSanPham.jar",
            draggable: true,
            width: 640,
            height: 480,
            code: "themsanpham.Main",
            name: "ThemSanPham"
        });
        $("#thongTinChiTiet").html(fxstring);
    }else{
        $.ajax({
            type: "POST",
            url: "/WebsiteBanHang/ThemSanPham",
            data: "command=layMauChiTietTheoMaNhomSanPham&MaNhomSanPham=" + nhomSanPham,
            success: function(msg){
                $("#thongTinChiTiet").html(msg);
            }
        });
    }
}

function validate(){
    var err = "";
    var allInput = $('input[type="text"]');    
    for (var i = 0; i < allInput.length; i++){
       
        if (allInput[i].value == ""){
            err += i;
            allInput[i].setAttribute("class", "notvalid");
        }
    }
    if (err == ""){
        return true;
    }
    return false;
}

function layDanhSachNhomSanPham(){
    $.ajax({
        type: "POST",
        url: "/WebsiteBanHang/ThemSanPham",
        data: "command=LayDanhSachNhomSanPham",
        success: function(msg){
            var str = "";
            $.each(msg, function(i, item){
                str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
            });
            str += "<option value=\"ThemMoi\">Thêm mới</option>";
            $("#nhomSanPham").html(str);
            layChiTietSanPham();
        }
    });
}

function layDanhSachDanhMucSanPham(){
    var sendData = "command=LayDanhMucSanPham";
    $.ajax({
        type: "POST",
        url: "/WebsiteBanHang/ThemSanPham",
        data: sendData,
        success: function(msg){
            var str = "<select id=\"selectDanhMucSanPham\" name=\"danhMucSanPham\">";
            $.each(msg, function(i, item){
                str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
            });
            str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemDanhMucSanPham\" value=\"Thêm mới\" onclick=\"themDanhMucSanPham();\"/>";
            $("#danhMucSanPham").html(str);
        }
    });
}

function themHangSanXuat(){
    var node = document.getElementById("themHangSanXuat");
    if (node != null){
        while (node.hasChildNodes()){
            node.removeChild(node.lastChild);
        }
        var formNode = document.createElement("form");
        formNode.setAttribute("name", "frmThemHangSanXuat");
        var tableNode = document.createElement("table");
        tableNode.setAttribute("width", "100%");
        var trNode = document.createElement("tr");
        var tdNode = document.createElement("td");
        tdNode.setAttribute("width", "150px");
        tdNode.innerHTML = "Mã hãng sản xuất";
        trNode.appendChild(tdNode);
        tdNode = document.createElement("td");
        var textBoxNode = document.createElement("input");
        textBoxNode.setAttribute("type", "text");
        textBoxNode.setAttribute("id", "maHangSanXuat");
        textBoxNode.setAttribute("width", "150px");
        tdNode.appendChild(textBoxNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);

        trNode = document.createElement("tr");
        tdNode = document.createElement("td");
        tdNode.innerHTML = "Tên hãng sản xuất";
        trNode.appendChild(tdNode);
        tdNode = document.createElement("td");
        textBoxNode = document.createElement("input");
        textBoxNode.setAttribute("type", "text");
        textBoxNode.setAttribute("id", "tenHangSanXuat");
        textBoxNode.setAttribute("width", "150px");
        tdNode.appendChild(textBoxNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);

        trNode = document.createElement("tr");
        tdNode = document.createElement("td");
        tdNode.setAttribute("colspan", "2");
        tdNode.setAttribute("align", "center");
        var btnNode = document.createElement("input");
        btnNode.setAttribute("type", "button");
        btnNode.setAttribute("name", "btnThemNhaSanXuat");
        btnNode.setAttribute("value", "Lưu");
        btnNode.setAttribute("onclick", "onThemHangSanXuat();");
        tdNode.appendChild(btnNode);
        btnNode = document.createElement("input");
        btnNode.setAttribute("type", "button");
        btnNode.setAttribute("name", "btnTroLai");
        btnNode.setAttribute("value", "Trở lại");
        btnNode.setAttribute("onclick", "onKhongThemNhaSanXuat();");
        tdNode.appendChild(btnNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);
        formNode.appendChild(tableNode);
        node.appendChild(formNode);
    }
}

function onThemHangSanXuat(){
    var maHangSanXuat = $("#maHangSanXuat").val();
    var tenHangSanXuat = $("#tenHangSanXuat").val();
    var err = "";
    if (maHangSanXuat.length == 0){
        $("#maHangSanXuat").addClass("notvalid");
        err += "Ma hang san xuat is empty";
    }
    if (tenHangSanXuat.length == 0){
        $("#tenHangSanXuat").addClass("notvalid");
        err += "ten hang san xuat is empty";
    }
    
    if (err == ""){
        var sendData = "command=ThemHangSanXuat&MaHangSanXuat=" + maHangSanXuat + "&TenHangSanXuat=" + tenHangSanXuat;
        $.ajax({
            type: "POST",
            url: "/WebsiteBanHang/ThemSanPham",
            data: sendData,
            success: function(msg){
                var str = "<select id=\"selectThemHangSanXuat\" name=\"hangSanXuat\">";
                $.each(msg, function(i, item){
                    str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
                });
                str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemNhaSanXuat\" value=\"Thêm mới\" onclick=\"themHangSanXuat();\"/>";
                $("#themHangSanXuat").html(str);                                 
            }
        });
    }else{
        alert(err);
    }
}

function onKhongThemNhaSanXuat(){
    var sendData = "command=LayDanhSachHangSanXuat";
    $.ajax({
        type: "POST",
        url: "/WebsiteBanHang/ThemSanPham",
        data: sendData,
        success: function(msg){
            var str = "<select id=\"selectThemHangSanXuat\" name=\"hangSanXuat\">";
            $.each(msg, function(i, item){
                str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
            });
            str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemNhaSanXuat\" value=\"Thêm mới\" onclick=\"themHangSanXuat();\"/>";
            $("#themHangSanXuat").html(str);
        }
    });
}

function themNhaPhanPhoi(){
    var node = document.getElementById("themNhaPhanPhoi");
    if (node != null){
        while (node.hasChildNodes()){
            node.removeChild(node.lastChild);
        }
        var formNode = document.createElement("form");
        formNode.setAttribute("name", "frmThemNhaPhanPhoi");
        var tableNode = document.createElement("table");
        tableNode.setAttribute("width", "100%");
        var trNode = document.createElement("tr");
        var tdNode = document.createElement("td");
        tdNode.setAttribute("width", "150px");
        tdNode.innerHTML = "Mã nhà phân phối";
        trNode.appendChild(tdNode);
        tdNode = document.createElement("td");
        var textBoxNode = document.createElement("input");
        textBoxNode.setAttribute("type", "text");
        textBoxNode.setAttribute("id", "maNhaPhanPhoi");
       
        tdNode.appendChild(textBoxNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);

        trNode = document.createElement("tr");
        tdNode = document.createElement("td");
        tdNode.innerHTML = "Tên nhà phân phối";
        trNode.appendChild(tdNode);
        tdNode = document.createElement("td");
        textBoxNode = document.createElement("input");
        textBoxNode.setAttribute("type", "text");
        textBoxNode.setAttribute("id", "tenNhaPhanPhoi");
       
        tdNode.appendChild(textBoxNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);

        trNode = document.createElement("tr");
        tdNode = document.createElement("td");
        tdNode.innerHTML = "Địa chỉ";
        trNode.appendChild(tdNode);
        tdNode = document.createElement("td");
        textBoxNode = document.createElement("input");
        textBoxNode.setAttribute("type", "text");
        textBoxNode.setAttribute("id", "diaChiNhaPhanPhoi");
        tdNode.appendChild(textBoxNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);

        trNode = document.createElement("tr");
        tdNode = document.createElement("td");
        tdNode.setAttribute("colspan", "2");
        tdNode.setAttribute("align", "center");
        var btnNode = document.createElement("input");
        btnNode.setAttribute("type", "button");
        btnNode.setAttribute("name", "btnThemNhaPhanPhoi");
        btnNode.setAttribute("value", "Lưu");
        btnNode.setAttribute("onclick", "onThemNhaPhanPhoi();");
        tdNode.appendChild(btnNode);
        btnNode = document.createElement("input");
        btnNode.setAttribute("type", "button");
        btnNode.setAttribute("name", "btnTroLai");
        btnNode.setAttribute("value", "Trở lại");
        btnNode.setAttribute("onclick", "onKhongThemNhaPhanPhoi();");
        tdNode.appendChild(btnNode);
        trNode.appendChild(tdNode);
        tableNode.appendChild(trNode);
        formNode.appendChild(tableNode);
        node.appendChild(formNode);
    }
}

function onThemNhaPhanPhoi(){
    var maNhaPhanPhoi = $("#maNhaPhanPhoi").val();
    var tenNhaPhanPhoi = $("#tenNhaPhanPhoi").val();
    var diaChiNhaPhanPhoi = $("#diaChiNhaPhanPhoi").val();
    var err = "";
    if (maNhaPhanPhoi.length == 0){
        $("#maNhaPhanPhoi").addClass("notvalid");
        err += "Ma nha phan phoi is empty";
    }
    if (tenNhaPhanPhoi.length == 0){
        $("#tenNhaPhanPhoi").addClass("notvalid");
        err += "ten nha phan phoi is empty";
    }
    if (diaChiNhaPhanPhoi.length == 0){
        $("#diaChiNhaPhanPhoi").addClass("notvalid");
        err += "dia chi nha phan phoi is empty";
    }
    if (err == ""){
        var sendData = "command=ThemNhaPhanPhoi&MaNhaPhanPhoi=" + maNhaPhanPhoi + "&TenNhaPhanPhoi=" + tenNhaPhanPhoi + "&DiaChi=" + diaChiNhaPhanPhoi;
        $.ajax({
            type: "POST",
            url: "/WebsiteBanHang/ThemSanPham",
            data: sendData,
            success: function(msg){
                var str = "<select id=\"selectThemNhaPhanPhoi\" name=\"nhaPhanPhoi\">";
                $.each(msg, function(i, item){
                    str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
                });
                str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemNhaPhanPhoi\" value=\"Thêm mới\" onclick=\"themNhaPhanPhoi();\"/>";
                $("#themNhaPhanPhoi").html(str);
            }
        });
    }
}

function onKhongThemNhaPhanPhoi(){
    var sendData = "command=LayDanhSachNhaPhanPhoi";
    $.ajax({
        type: "POST",
        url: "/WebsiteBanHang/ThemSanPham",
        data: sendData,
        success: function(msg){
            var str = "<select id=\"selectThemNhaPhanPhoi\" name=\"nhaPhanPhoi\">";
            $.each(msg, function(i, item){
                str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
            });
            str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemNhaPhanPhoi\" value=\"Thêm mới\" onclick=\"themNhaPhanPhoi();\"/>";
            $("#themNhaPhanPhoi").html(str);
        }
    });
}

function themDanhMucSanPham(){
    var str = "<input type=\"text\" id=\"tenDanhMucSanPham\"/><input type=\"button\" value=\"Lưu\" onclick=\"onThemDanhMucSanPham();\"/><input type=\"button\" value=\"Trở lại\" onclick=\"layDanhSachDanhMucSanPham();\"/>";
    $("#danhMucSanPham").html(str);
    
}

function onThemDanhMucSanPham(){
    var tenDanhMuc = $("#tenDanhMucSanPham").val();

    if (tenDanhMuc.length > 0){
        var sendData = "command=ThemDanhMucSanPham&tenDanhMucSanPham=" + tenDanhMuc;
        $.ajax({
            type: "POST",
            url: "/WebsiteBanHang/ThemSanPham",
            data: sendData,
            success: function(msg){
                var str = "<select id=\"selectDanhMucSanPham\" name=\"danhMucSanPham\">";
                $.each(msg, function(i, item){
                    str += "<option value=\"" + item[0] + "\">" + item[1] + "</option>"
                });
                str += "</select> &nbsp; &nbsp;<input type=\"button\" name=\"btnThemDanhMucSanPham\" value=\"Thêm mới\" onclick=\"themDanhMucSanPham();\"/>";
                $("#danhMucSanPham").html(str);
            }
        });
    }
    else{
        alert("Bạn chưa nhập tên danh mục");
    }
}
